package com.ericdm.callingandsmsstatistics;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper
{
	public static final int VERSION = 1;	
	private static final String CREATETABLESQL_OUTGOINGCALL = "create table if not exists CallingAndSmsHistoryTableOUTGOINGCALL(PHONENUMBER CHAR(20) NOT NULL,OUT_GOING_CALL CHAR(5), DATE_ CHAR(11), DATEFULL CHAR(20))";
	private static final String CREATETABLESQL_INCOMMINGCALL = "create table if not exists CallingAndSmsHistoryTableINCOMINGCALL(PHONENUMBER CHAR(20) NOT NULL,INCOMMING_CALL CHAR(5), DATE_ CHAR(11), DATEFULL CHAR(20))";
	private static final String CREATETABLESQL_SENDEDSMS = "create table if not exists CallingAndSmsHistoryTableSENDEDSMS(PHONENUMBER CHAR(20) NOT NULL,SENDED_SMS CHAR(5), DATE_ CHAR(11), DATEFULL CHAR(20))";
	private static final String CREATETABLESQL_RECEVIEDSMS = "create table if not exists CallingAndSmsHistoryTableRECEVIEDSMS(PHONENUMBER CHAR(20) NOT NULL,RECEVIED_SMS CHAR(5), DATE_ CHAR(11), DATEFULL CHAR(20))";
	private static final String CREATETABLESQL_QUREYTIME = "create table if not exists CallingAndSmsHistoryTableQUERYTIME(STARTTIME CHAR(20), ENDTIME CHAR(20))";
	public static final String CREATETABLESQL_PHONENUMBERINCOMING = "create table if not exists CallingAndSmsHistoryTablePHONENUMBERINCOMING(PHONENUMBER CHAR(20) NOT NULL,PHONEINCOMINGTIME INTEGER)";
	public static final String CREATETABLESQL_PHONENUMBEROUTGOING = "create table if not exists CallingAndSmsHistoryTablePHONENUMBEROUTGOING(PHONENUMBER CHAR(20) NOT NULL,PHONEOUTGOINGTIME INTEGER)";
	public static final String CREATETABLESQL_PHONENUMBERSENDSMS = "create table if not exists CallingAndSmsHistoryTablePHONENUMBERSENDSMS(PHONENUMBER CHAR(20) NOT NULL,PHONESENDSMS INTEGER)";
	public static final String CREATETABLESQL_PHONENUMBERRECIVESMS = "create table if not exists CallingAndSmsHistoryTablePHONENUMBERRECIVESMS(PHONENUMBER CHAR(20) NOT NULL,PHONERECEIVESMS INTEGER)";
	public static final String CREATETABLESQL_SUMINCOMMING = "create table if not exists CallingAndSmsHistoryTableSUMINCOMING(SUM_INCOMING INTEGER)";
	public static final String CREATETABLESQL_SUMOUTGOING = "create table if not exists CallingAndSmsHistoryTableSUMOUTGOING(SUM_OUTGOING INTEGER)";
	public static final String CREATETABLESQL_SUMSENDSMS = "create table if not exists CallingAndSmsHistoryTableSUMSENDSMS(SUM_SENDSMS INTEGER)";
	public static final String CREATETABLESQL_RECEISMS = "create table if not exists CallingAndSmsHistoryTableSUMRECEIVESMS(SUM_RECEIVESMS INTEGER)";
	
	/**  
	 * 在SQLiteOpenHelper的子类当中，必须有该构造函数
	 * @param context   上下文对象
	 * @param name      数据库名称
	 * @param factory
	 * @param version   当前数据库的版本，值必须是整数并且是递增的状态
	 */
	public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
	{
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	
	public DatabaseHelper(Context context, String name, int version)
	{
		this(context,name,null,version);  
	}
	
	public DatabaseHelper(Context context, String name)
	{
		this(context,name,VERSION);  
	}

	//该函数是在第一次创建的时候执行，实际上是第一次得到SQLiteDatabase对象的时候才会调用这个方法
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// TODO Auto-generated method stub
		try
		{
			db.execSQL(CREATETABLESQL_OUTGOINGCALL);
			db.execSQL(CREATETABLESQL_INCOMMINGCALL);
			db.execSQL(CREATETABLESQL_SENDEDSMS);
			db.execSQL(CREATETABLESQL_RECEVIEDSMS);
			db.execSQL(CREATETABLESQL_QUREYTIME);
			db.execSQL(CREATETABLESQL_PHONENUMBERINCOMING);
			db.execSQL(CREATETABLESQL_PHONENUMBEROUTGOING);
			db.execSQL(CREATETABLESQL_PHONENUMBERSENDSMS);
			db.execSQL(CREATETABLESQL_PHONENUMBERRECIVESMS);
			db.execSQL(CREATETABLESQL_SUMINCOMMING);
			db.execSQL(CREATETABLESQL_SUMOUTGOING);
			db.execSQL(CREATETABLESQL_SUMSENDSMS);
			db.execSQL(CREATETABLESQL_RECEISMS);
		}
		catch (Exception e)
		{
			System.out.println("Create database table error!");
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub
	}

}
